{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is the Python code used to convert .wav files to spectrograms\n",
    "\n",
    "Audio data is from this [Kaggle dataset](https://www.kaggle.com/c/freesound-audio-tagging/data), which is from this [DCASE 2018 paper](https://arxiv.org/abs/1807.09902)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from scipy import signal\n",
    "from scipy.io import wavfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prefix = 'gs://path/to/audio/files'\n",
    "spectro_prefix = 'gs://path/to/spectrograms'\n",
    "\n",
    "for i in range(len(names)):\n",
    "    fname = prefix + names[i]\n",
    "    !gsutil cp $fname .\n",
    "\n",
    "    # Create spectrogram with scipy\n",
    "    sample_rate, samples = wavfile.read(names[i])\n",
    "    freq, times, spectro = signal.spectrogram(samples, sample_rate)\n",
    "    \n",
    "    # Create specrogram image with matplotlib\n",
    "    plt.figure(figsize=(28,28), frameon=False)\n",
    "    fig = plt.gcf()\n",
    "    plt.axis('off')\n",
    "    plt.pcolormesh(times, freq, np.log(spectro))\n",
    "\n",
    "    # Save spectrogram to local file\n",
    "    name = names[i].split('.')[0]\n",
    "    fig.savefig(name)\n",
    "    spectro_path = spectro_prefix + name + '.png'\n",
    "    local_path = name + '.png'\n",
    "\n",
    "    # Convert image to 3 channels and shrink\n",
    "    im = Image.open(local_path)\n",
    "    im.thumbnail((128,128), Image.ANTIALIAS)\n",
    "    im.convert(mode='RGB')\n",
    "    im.save(local_path)\n",
    "\n",
    "    # Save spectrogram to GCS and remove local files\n",
    "    !gsutil cp $local_path $spectro_path\n",
    "    !rm $local_path\n",
    "    local_audio = names[i]\n",
    "    !rm $local_audio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright 2020 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37464bit08b76e2ac8a34eb59e2287d9d74f34e3",
   "display_name": "Python 3.7.4 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}